package com.kakarote.oa.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.alibaba.fastjson.JSONObject;
import com.kakarote.common.field.utils.FieldUtil;
import com.kakarote.core.common.Const;
import com.kakarote.core.servlet.BaseServiceImpl;
import com.kakarote.core.utils.UserUtil;
import com.kakarote.oa.entity.PO.OaLogData;
import com.kakarote.oa.entity.VO.OaLogTemplateFieldVO;
import com.kakarote.oa.mapper.OaLogDataMapper;
import com.kakarote.oa.service.IOaLogDataService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author wanli
 * @description 针对表【wk_oa_log_template_data(日志模板扩展字段数据表)】的数据库操作Service实现
 * @createDate 2022-07-06 10:03:19
 */
@Service
public class OaLogDataServiceImpl extends BaseServiceImpl<OaLogDataMapper, OaLogData> implements IOaLogDataService {


    @Override
    public void setData(JSONObject crmModel, Long id) {
        List<OaLogData> baseData = this.lambdaQuery().eq(OaLogData::getDataId, id).list();
        for (OaLogData data : baseData) {
            crmModel.put(data.getName(), data.getValue());
        }
    }

    /**
     * 批量查询数据
     *
     * @param dataIds ids
     * @return data
     */
    @Override
    public Map<Long, JSONObject> queryBatchData(List<Long> dataIds) {
        Map<Long, List<OaLogData>> longListMap = lambdaQuery().in(OaLogData::getDataId, dataIds).list().stream().collect(Collectors.groupingBy(OaLogData::getDataId));
        Map<Long, JSONObject> dataMap = new HashMap<>(longListMap.size());
        for (Long key : longListMap.keySet()) {
            List<OaLogData> logData = longListMap.get(key);
            JSONObject jsonObject = new JSONObject(logData.size());
            for (OaLogData oaLogData : logData) {
                jsonObject.put(oaLogData.getName(), oaLogData.getValue());
            }
            dataMap.put(key, jsonObject);
        }
        return dataMap;
    }


    /**
     * 保存自定义字段数据
     *
     * @param array data
     */
    @Override
    public void saveData(List<OaLogTemplateFieldVO> array, Long logId) {
        List<OaLogData> dataList = new ArrayList<>();
        this.lambdaUpdate().eq(OaLogData::getDataId, logId).remove();
        for (OaLogTemplateFieldVO obj : array) {
            OaLogData data = BeanUtil.copyProperties(obj, OaLogData.class);
            data.setDataId(logId);
            data.setValue(FieldUtil.format2DbString(obj.getValue(), obj.getType(), data.getValue()));
            data.setName(obj.getFieldName());
            data.setCreateTime(LocalDateTimeUtil.now());
            dataList.add(data);
        }
        saveBatch(dataList, Const.BATCH_SAVE_SIZE);
    }

    /**
     * 通过batchId删除数据
     *
     * @param logIds data
     */
    @Override
    public void deleteByDataId(List<Long> logIds) {
        this.lambdaUpdate()
                .in(OaLogData::getDataId, logIds)
                .remove();
    }

    @Override
    public List<Long> getDataIdsBySearch(String search) {
        return this.getBaseMapper().getDataIdsBySearch(search);
    }
}




